using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace SunshineFarmWord
{
    public class TreeScript : InteractiveBase
    {
        private Vector3 originalPosition;  // 原始位置
        //抖动节点
        [field: SerializeField, Tooltip("抖动节点")]
        private GameObject shakeNode;
        //树
        [field: SerializeField, Tooltip("树")]
        private GameObject treeNodeSkin;
        //树桩
        [field: SerializeField, Tooltip("树桩")]
        private GameObject treeStumpNodeSkin;
        // 掉落物的预制体
        [field: SerializeField, Tooltip("掉落物预制体")]
        private GameObject droppedItemsPrefab;

        // 需要砍树的次数
        private int _cutsTreeNumber = 0;

        private int _systemTimeEventId = -1;
        // 自动恢复树时间
        private float resetTreeTime;
        protected override void Start()
        {
            base.Start();
            originalPosition = shakeNode.transform.position;
        }

        #region Base functions
        public override void InteractiveStart(PlayerStateMachine stateMachine)
        {
            base.InteractiveStart(stateMachine);
            stateMachine.ChangeState(stateMachine.AxeState);
        }
        #endregion

        //砍树
        // 参数 玩家的朝向的方向
        public void CuttingDownTrees(Vector2 playerFaceDirection)
        {
            _cutsTreeNumber--;
            if (_cutsTreeNumber <= 0)
            {
                //砍完了
                _cutsTreeNumber = 0;

                //生成掉落物品
                GameObject treeDroppedPrefab = Instantiate(droppedItemsPrefab);
                DroppedItemsBase droppedItemsBaseScript = treeDroppedPrefab.GetComponent<DroppedItemsBase>();
                droppedItemsBaseScript.GenerateDroppedItems(GameObjEnumClass.DroppedItemsEnum.Tree);
                droppedItemsBaseScript.MoveTransfromTo(transform.position, playerFaceDirection * 1.5f);


                // 树是显示状态 隐藏树 显示树桩
                // 树是隐藏状态 销毁树
                if (treeNodeSkin.activeSelf)
                {
                    //隐藏树
                    //显示树桩
                    treeNodeSkin.SetActive(false);
                    treeStumpNodeSkin.SetActive(true);

                    //树恢复
                    StartAutoResetTree();
                }
                else
                {
                    CancelAutoResetTree();
                    //销毁树节点
                    Destroy(gameObject);
                }
            }
        }

        // 抖动持续时间 抖动幅度
        public void Shake(float duration, float amount = 0.05f)
        {
            if (_cutsTreeNumber == 0) return;
            StartCoroutine(ShakeCoroutine(duration, amount));
        }

        private IEnumerator ShakeCoroutine(float duration, float amount)
        {
            float elapsed = 0f;
            while (elapsed < duration)
            {
                shakeNode.transform.position = originalPosition + (Vector3)Random.insideUnitCircle * amount;
                elapsed += Time.deltaTime;
                yield return null;
            }
            shakeNode.transform.position = originalPosition; // 结束抖动，恢复原始位置
        }

        //生成砍树次数
        public int GenerateCutsTreeNumber(int cutsTreeMinNumber, int cutsTreeMaxNumber)
        {
            //如果已经生成过，则直接返回
            if (_cutsTreeNumber > 0) return _cutsTreeNumber;
            //生成砍树次数
            _cutsTreeNumber = Random.Range(cutsTreeMinNumber, cutsTreeMaxNumber);
            //如果是砍树树桩，则砍树次数翻倍
            if (!treeNodeSkin.activeSelf) _cutsTreeNumber *= 2;
            return _cutsTreeNumber;
        }


        //显示树
        public void ShowTree()
        {
            shakeNode.SetActive(true);
        }
        //隐藏树
        public void HideTree()
        {
            shakeNode.SetActive(false);
        }

        //恢复树
        public void ResetTree()
        {
            //树显示的时候将需要砍树次数设置为0
            //树桩显示的时候恢复树
            if (treeStumpNodeSkin.activeSelf)
            {
                //恢复树
                treeNodeSkin.SetActive(true);
                treeStumpNodeSkin.SetActive(false);
            }
            //恢复树
            _cutsTreeNumber = 0;
        }

        //取消自动恢复
        public void CancelAutoResetTree()
        {
            if (_systemTimeEventId == -1) return;
            SystemTimeManage.Instance.RemoveTimeEvent(_systemTimeEventId);
            _systemTimeEventId = -1;
        }

        //开启自动恢复
        public void StartAutoResetTree()
        {
            _systemTimeEventId = SystemTimeManage.Instance.AddTimeEvent(resetTreeTime, ResetTree);
        }

        //设置恢复时间
        public void SetResetTreeTime(float resetTreeTime)
        {
            this.resetTreeTime = resetTreeTime;
        }

        #region 碰撞触发
        void OnTriggerEnter2D(Collider2D collider)
        {
        }

        void OnTriggerExit2D(Collider2D collider)
        {
        }
        #endregion
    }
}
